package com.whitedust.top70;

/**
 *
 * @author A
 * @since 2025/11/11 12:55
 */

public class Top63 {
    public int[] searchRange(int[] nums, int target) {
        if (nums == null || nums.length == 0) return new int[]{-1, -1};
        return binarySearch(nums, target);
    }

    private int[] binarySearch(int[] nums, int target) {
        int start = 0;
        int end = nums.length - 1;
        while (start <= end) {
            int mid = start + (end - start) / 2;
            if (nums[mid] == target) {
                int left = mid;
                int right = mid;
                while (left >= start && nums[left] == target) {
                    left--;
                }
                while (right <= end && nums[right] == target) {
                    right++;
                }
                return new int[]{left + 1, right - 1};
            } else if (nums[mid] > target) {
                end = mid - 1;
            } else {
                start = mid + 1;
            }
        }
        return new int[]{-1, -1};
    }
}
